/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.thread;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.portal.common.oscache.CacheHelper;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * description: 导入任务线程类
 *
 * @author qihan 创建时间：2017-9-14
 */
public class ExcelImpTask implements Callable<Integer> {

    private List<Map<String, String>> impMap;

    private Map<String, String> reqs;

    private LoginUser loginUser;

    public ExcelImpTask(Map<String, String> reqs, List<Map<String, String>> impMap,
            LoginUser loginUser) {
        this.impMap = impMap;
        this.reqs = reqs;
        this.loginUser = loginUser;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see java.util.concurrent.Callable#call()
     */
    public Integer call() throws Exception {
        int count = 0;
        try {
            buildParams();
            Map<String, String> params = null;
            Map<String, String> mm = impMap.get(0);
            int num = mm.size();
            String splitAttr = "";
            boolean mark = true;
            int result = impMap.size();
            int j = 1;
            String beanId = PortalPubFactory.getInstance().getBeanId(reqs.get("datasource"));
            do {
                params = new HashMap<String, String>();
                params.put("sqlKeyId", "sqlKeyId");
                if (ToolsUtil.isNotNull(reqs.get("rules"))) {
                    params.put("sqlKeyId", reqs.get("rules"));
                    if (Validator.isNotNull(reqs.get("sqlKeyId"))) {
                        params.put(reqs.get("sqlKeyId"), reqs.get("rules"));
                    } else {
                        params.put("beforMethodKey", reqs.get("rules"));
                    }
                }
                if (ToolsUtil.isNotNull(reqs.get("ifBatch"))) {
                    params.put("ifBatch", reqs.get("ifBatch"));
                }
                params.put("resultType", "processSql");
                params.put("loginUser_userEname", loginUser.getUserEname());//确保其他表中没有该字段
                params.put("loginUser_orgId", loginUser.getUser().getOrgId());//确保其他表中没有该字段
                params.put("modelTypes", reqs.get("modelTypes"));
                params.put("sysPass", ConfigUtil.getInstance().interfacePass());
                params.put("datasource", reqs.get("datasource"));
                List<Map<String, String>> subList = null;
                if (100 >= result) {
                    subList = impMap;
                    mark = true;
                } else {
                    int nowNum = j * 200;
                    int startNum = (j - 1) * 200;
                    if (nowNum >= result) {
                        nowNum = result;
                        subList = impMap.subList(startNum, nowNum);
                        mark = true;
                    } else {
                        j++;
                        subList = impMap.subList(startNum, nowNum);
                    }
                }
                for (Map<String, String> imp : subList) {
                    int i = 1;
                    for (String key : imp.keySet()) {
                        String value = StrUtil.doNull(imp.get(key), "-");
                        if (i == 1) {
                            splitAttr = key.replace(reqs.get("objName") + ".", "");
                            params.put(reqs.get("objName") + ".splitAttr", splitAttr);
                        }
                        params.put(key, StrUtil.isNull(params.get(key)) ?
                                value :
                                params.get(key) + "-#-" + value);
                        i++;
                    }
                }
                Map map = params;
                ResultEntity entity = LoadRpcService.service()
                        .doServiceClient(beanId, "dymicSql", map,
                                PortalPubFactory.getInstance().getRpcInfo("ius"));
                if (StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
                    count += subList.size();
                    if (count == result) {
                        mark = false;
                    }
                } else {
                    j--;
                }
            } while (mark);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return count;
    }

    private void buildParams() {
            String pageName = reqs.get("pageName");
            String formName = reqs.get("formName");
            String key = formName + "_" + pageName;
            Map map = (Map) CacheHelper.getInstance().getIusParamCache(key);
            String modelTypes = String.valueOf(map.get("modelTypes"));
            reqs.put("datasource", String.valueOf(map.get("sysName")));
            reqs.put("modelTypes", modelTypes);
            if (modelTypes.startsWith("i_")) {
                reqs.put("objName", modelTypes.substring(2));
            } else if (modelTypes.startsWith("ib_")) {
                reqs.put("objName", modelTypes.substring(3));
                reqs.put("ifBatch", "T");
            }
            String rules = String.valueOf(map.get("rulesName"));
            reqs.put("rules", rules);
            String evelName = String.valueOf(map.get("evelName"));//方法拦截方式
            reqs.put("sqlKeyId", evelName);
    }
}
